package edu.scut.course_management.controller;

import edu.scut.course_management.dto.RequestForResourceDTO;
import edu.scut.course_management.exception.CustomException;
import edu.scut.course_management.po.Resource;
import edu.scut.course_management.po.Section;
import edu.scut.course_management.result.JsonResult;
import edu.scut.course_management.result.JsonResultFactory;
import edu.scut.course_management.result.ResultCode;
import edu.scut.course_management.service.ResourceManagementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 课程资源管理控制器
 * @author shawshank
 */
@RestController
@RequestMapping("Course/Resource")
public class ResourceManagementController {

    @Autowired
    private ResourceManagementService resourceManagementService;

    /**
     * 上传文件资源
     * URL：http://localhost:8080/Course/Resource/CreateResourceNode
     */
    @PostMapping("/CreateResourceNode")
    @PreAuthorize("hasAuthority('teacher')")
    public JsonResult createResourceNode(@RequestBody RequestForResourceDTO requestForResourceDTO) {
        JsonResult jsonResult = null;
        JsonResultFactory jsonResultFactory = JsonResultFactory.getInstance();
        // 检测参数有效性
        if (!requestForResourceDTO.checkParamValidity(true)) {
            jsonResult = jsonResultFactory.buildFailResult(ResultCode.PARAM_NOT_COMPLETE);
        } else {
            List<Section> sections = requestForResourceDTO.getSections();
            Resource resourceNode = requestForResourceDTO.getResourceNode();
            resourceNode.setName(resourceNode.getName().trim());
            Integer parentNodeId = requestForResourceDTO.getNodeId();
            try {
                List<Resource> result = resourceManagementService.addResourceNode(sections, resourceNode, parentNodeId);
                jsonResult = jsonResultFactory.buildSuccessResult(result);
            } catch (CustomException e) {
                jsonResult = jsonResultFactory.buildFailResult(e.getResultCode());
            }
        }
        return jsonResult;
    }

    /**
     * 获得下一级文件列表
     * URL：http://localhost:8080/Course/Resource/GetFileListOfNextLevel
     */
    @PostMapping("/GetFileListOfNextLevel")
    public JsonResult getFileListOfNextLevel(@RequestBody RequestForResourceDTO requestForResourceDTO) {
        JsonResult jsonResult = null;
        JsonResultFactory jsonResultFactory = JsonResultFactory.getInstance();
        // 检测参数有效性
        if (!requestForResourceDTO.checkParamValidity(false)) {
            jsonResult = jsonResultFactory.buildFailResult(ResultCode.PARAM_NOT_COMPLETE);
        } else {
            List<Section> sections = requestForResourceDTO.getSections();
            Integer parentNodeId = requestForResourceDTO.getNodeId();
            try {
                List<Resource> resourceNodesOfNextLevel = resourceManagementService.getFileListOfNextLevel(sections, parentNodeId);
                jsonResult = jsonResultFactory.buildSuccessResult(resourceNodesOfNextLevel);
            } catch (CustomException e) {
                jsonResult = jsonResultFactory.buildFailResult(e.getResultCode());
            }
        }
        return jsonResult;
    }

    /**
     * 删除课程资源
     * URL：http://localhost:8080/Course/Resource/DeleteResourceNode
     */
    @PostMapping("/DeleteResourceNode")
    @PreAuthorize("hasAuthority('teacher')")
    public JsonResult deleteResourceNode(@RequestBody RequestForResourceDTO requestForResourceDTO) {
        JsonResult jsonResult = null;
        JsonResultFactory jsonResultFactory = JsonResultFactory.getInstance();
        // 检测参数有效性
        if (!requestForResourceDTO.checkParamValidity(false)) {
            jsonResult = jsonResultFactory.buildFailResult(ResultCode.PARAM_NOT_COMPLETE);
        } else {
            List<Section> sections = requestForResourceDTO.getSections();
            Integer parentNodeId = requestForResourceDTO.getNodeId();
            try {
                List<Resource> result = resourceManagementService.deleteResourceNode(sections, parentNodeId);
                jsonResult = jsonResultFactory.buildSuccessResult(result);
            } catch (CustomException e) {
                jsonResult = jsonResultFactory.buildFailResult(e.getResultCode());
            }
        }
        return jsonResult;
    }


}
